﻿/*----------------------------------------------------------------------------------------
*
*  Copyright 2019, Gao Hai Hui, <fromzeropoint@126.com>.  All rights reserved.
*  https://gitee.com/helloworldghh/xoskit.git
*  Use of this source code is governed by a MIT license
*  that can be found in the License file.
*
----------------------------------------------------------------------------------------*/
#ifndef __IMPL_SSL_IMPL_H__
#define __IMPL_SSL_IMPL_H__

#include "include_openssl.h"
#include "../tools/head.h"

namespace xos_encrypt
{

    class ssl_impl : public xos_stl::mem_item< xos::release_impl< ssl_impl >, thread_lock >,
		public i_ssl
    {
    public:
        typedef xos::release_impl< ssl_impl > T;

    public:
        /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        //
        static int get_addr( POOL *** pppPool, LIST *** pppList );

    public:
        ssl_impl();
        ~ssl_impl();

    protected:
        xos_box::i_buf * m_pWriteBuf;
        xos_box::i_buf * m_pReadBuf;

        BUF_MEM m_write_buf;
        BUF_MEM m_read_buf;

        SSL * m_pSSL;

        BIO * m_pWriteBIO;
        BIO * m_pReadBIO;

        bool m_bIsServer;
        bool m_bReady;

    public:
        int init();
        int term();

    protected:
        int get_from_mem( xos_box::i_list * pList );
        int add_to_mem( xos_box::i_list * pList );

        int get_from_ssl( xos_box::i_list * pList );
        int add_to_ssl( xos_box::i_list * pList );

        int show_error_info( int nRet );
        int write_error( int nRet );
        int read_error( int nRet );

        int init_null_mem( BUF_MEM * pMem );
        bool empty_mem( BUF_MEM * pMem );
        bool null_mem( BUF_MEM * pMem );

        int init_mem( BUF_MEM * pMem );
        int term_mem( BUF_MEM * pMem );

        int init_data();

    public:
        // 
        // i_ssl methods
        // 
        //  0 : 成功
        // -1 : 失败
        int shake_hand( xos_box::i_list * pIn, xos_box::i_list * pOut );

        //  0 : 成功
        // -1 : 失败
        int shutdown( xos_box::i_list * pIn, xos_box::i_list * pOut );

        //  0 : 成功
        // -1 : 失败
        // -2 : 关闭
        int decode( xos_box::i_list * pIn, xos_box::i_list * pOut );

        //  0 : 成功
        // -1 : 失败
        int encode( xos_box::i_list * pIn, xos_box::i_list * pOut );

        //  服务端或客户端
        int set_type( bool bServer );

        // 初始化
        int initialize();

        // 可以通迅了
        bool ready();
    };

} // xos_encrypt

#endif // __IMPL_SSL_IMPL_H__
